from django.http import HttpResponse
from django.template.loader import get_template  # 加载模板文件的方法
from django.shortcuts import render_to_response,render  # 加载并渲染
from django.db.models import Max,Min,Count,Avg,Sum,Q,F
from News.models import *
import time
def geet(request,month,day):
    birthday = time.mktime((2019, int(month), int(day), 0, 0, 0, 0, 0, 0))
    dayth = time.localtime(birthday).tm_yday
    html = """
        <html>
            <head>
                <title>
                </title>
                <style>
                    .content {
                       color: red;
                       text-align: center;
                    }
                </style>
            </head>
            <body>
                <h1 align="center">算算你的生日是今年的第几天</h1>
                <p class='content'>%s</p>
            </body>
        </html>
    """
    string = "你的生日是 %s 月 %s 日,是今年的第%s天"%(month,day,dayth)
    return HttpResponse(html%string)

def cheng(request):
    a = '<table>'
    b = '<tr>'
    for x in range(1,10):
        for y in range(1,x + 1):
            if y % 2 != 0:
                b += '<td style="background: red;color: pink;">{}×{}={}</td>'.format(y,x,x*y)
            else:
                b += '<td style="background: yellow;color: blue;">{}×{}={}</td>'.format(y, x, x * y)
        b += '</tr>'
    a += b + '</table>'
    return HttpResponse(a)

def fengche(request):
    pass

def che(request):
    template = get_template('index.html')
    response = template.render()
    return  HttpResponse(response)

def taiji(request):
    template = get_template('tj.html')  # 加载步骤
    response = template.render()  # 渲染
    return HttpResponse(response)  #返回一个响应, 响应的内容时是一个渲染的结果

def ym(request):
    template = get_template('ym.html')  # 加载步骤
    response = template.render(
        {
            'name':'laozhang'
        }
    )  # 渲染
    return HttpResponse(response)  #返回一个响应, 响应的内容时是一个渲染的结果

def ym11(request):
    return render_to_response(
        'ym.html',
        {

        }
    )  #返回一个响应, 响应的内容时是一个渲染的结果

class Example:
    age = 123456
    def cc(self):
        a = 10
        return locals()

def index_page(request):
    """
    当前视图加载index.html,index.html的路径来源于settings当中的TEMPLATES配置
    """
    #加载前端页面
    template = get_template("ym.html") #加载的步骤
    #将后端数据渲染到前端页面上
    response = template.render(
        {
            "name":"laozhang",
            "list_name":[1,2,3,4],
            "tuple_name":(1,2,3,4),
            "dict_name": {"a":1},
            "fun_name": lambda x: x,
            "class_name": Example
         }
    ) #渲染数据
    #返回渲染结果
    return HttpResponse(response) #返回一个响应，响应的内容时一个渲染的结果

def tm(request):
    name = 'laozhang'
    list_name = [1,2,3,4]
    tuple_name = (1,2,3,4)
    dict_name = {'age': 18}
    fun_name = lambda x: x
    class_name = Example
    return render_to_response('ym.html',locals())

def tmhttp(request):
    res = [
        {'name':'老王','age':'18'},
        {'name': '1', 'age': '18'},
        {'name': '2', 'age': '18'},
        {'name': '3', 'age': '18'},
        {'name': '4', 'age': '18'},
    ]
    return render_to_response('nfsq.html',locals())

def tam(request):
    res = [
        {'img': '../static/img/ms-pic1.jpg', 'name': '海鲜芝士大虾', 'price': '价格：49元'},
        {'img': '../static/img/ms-pic2.jpg', 'name': '草莓布丁杯', 'price': '价格：12元'},
        {'img': '../static/img/ms-pic3.jpg', 'name': '菲力黑椒牛排', 'price': '价格：69元'},
        {'img': '../static/img/ms-pic4.jpg', 'name': '香煎排意面', 'price': '价格：69元'},
        {'img': '../static/img/ms-pic5.jpg', 'name': '鲜香培根比萨', 'price': '价格：59元/24寸'},
        {'img': '../static/img/ms-pic6.jpg', 'name': '番茄烩意面', 'price': '价格：39元'}
    ]
    return render_to_response('meishi.html', locals())

def add_type(request):
    news_type = NewsType()
    news_type.label = '杨鑫诗歌'
    news_type.description = '杨鑫唱的歌'
    news_type.save()
    return HttpResponse('保存杨鑫成功!')

def add_News(request):
    news = News()
    news.title = '杨鑫标题'
    news.time = '2019-10-10'
    news.description = '新闻描述'
    news.image = '1.jpg'
    news.content = '新闻内容'
    news.type_id = NewsType.objects.get(id=1)
    news.save()
    return HttpResponse('保存杨鑫成功!')

def selectExample(request):
    news_list = NewsType.objects.get(id = 1).news_set.all()
    return render_to_response('yangxin.html',locals())

def sese(request):
    news = News.objects.all().aggregate(
        id_avg=Avg("id"),id_count=Count("id"),id_sum=Sum("id"),id_max=Max("id"),id_min=Min("id")
    )

    return render_to_response('yangxin.html',locals())

def Examplell(request):
    # news = News.objects.raw('select * from news_')
    # method = dir(news)
    # news = News.objects.values("title").all()
    # news = News.objects.all().aggregate(
    #     id_avg=Avg('id'),id_count=Count('id'),id_sum=Sum('id'),id_max=Max('id'),id_min=Min('id')
    # )
    # news = News.objects.all().annotate(min_id=Min('id')).values('min_id')
    # news = News.objects.values('title').annotate(Count('title'))
    news = News.objects.filter()
    return render_to_response('qwe.html',locals())



